package veilingInterceptors;

import java.util.Map;

import veilingAware.UserAware;
import veilingDomein.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

@SuppressWarnings("serial")
public class ManagerAuthenticationInterceptor implements Interceptor {

	@Override
	public void destroy() {

	}

	@Override
	public void init() {

	}

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		@SuppressWarnings("rawtypes")
		Map session = actionInvocation.getInvocationContext().getSession();

		User user = (User) session.get("user");

		if (user == null) {
			return Action.LOGIN;
		} else if (user != null && user.getRechten() == 0) {
			Action action = (Action) actionInvocation.getAction();
			if (action instanceof UserAware) {
				((UserAware) action).setUser(user);
			}
			return actionInvocation.invoke();
		}
		return Action.LOGIN;

	}
}
